// pages/component/category/category.js
var loadMoreView, page
const http = require('../../utils/http')
var Api = require("../../common/api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      '/image/1.jpg',
      '/image/2.jpg',
      '/image/3.jpg'
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 4000,
    duration: 800,

//------------------------------------

    feed: [],
    feed_length: 0,
    userId: 0, //当前用户ID

    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    scrollTop: 0,
    pageCarousels: {}, //图片轮播

    system: {}, //系统信息
    items: [],
    resultData: [], // 存放数据
    selectedView: 'article',
    page: 0,

    thumbUpButtonFalse:"../../../image/icon/dianzan.png",
    thumbUpButtonTrue: "../../../image/icon/dianzan_default.png",

    //当前登录用户的openid
    openid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    page = 0;
    loadMoreView = this.selectComponent("#loadMoreView");
    this.loadData('article', true);
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onShow: function () {
   this.setData({
     openid: wx.getStorageSync("openid"),
   });
  },


  loadData: function (viewType, showLoading) {
    console.log("加载。。。。。");

    var that = this;
    http.post({
      url: Api.host + '/api/comm/discover',
      data: {
        page: page,
        limit: 20,
        openid: wx.getStorageSync('openid'),
      },
      showLoading: showLoading,
      success: (res) => {
        var items = that.data.resultData;
        if (page == 0) {
          items = res.topicsInfos;
          wx.stopPullDownRefresh()
        } else {
          items = items.concat(res.topicsInfos);
        }
        that.setData({
          items: items,
          resultData: items,
          selectedView: viewType,
          pageCarousels: res.carousels,
        })

        loadMoreView.loadMoreComplete(res.topicsInfos)
      },
      fail: () => {
        if (page != 0) {
          loadMoreView.loadMoreFail()
        }
      }
    })
  },
  loadMoreListener: function (e) {
    page += 1
    this.loadData(this.data.selectedView, false)
  },
  clickLoadMore: function (e) {
    this.loadData(this.data.selectedView, false)
  },



  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    page = 0
    this.loadData(this.data.selectedView, false)
  },

  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    loadMoreView.loadMore()
  },

  onPageScroll: function (e) {
    console.log(e)
    if (e.scrollTop >= this.data.system.windowWidth / 9 * 5 && !this.data.float) {
      this.setData({
        float: true
      })
    } else if (e.scrollTop < this.data.system.windowWidth / 9 * 5 && this.data.float) {
      this.setData({
        float: false
      })
    }
  },
 
  toSharespace:function(e){
    if (this.data.openid == null || this.data.openid == ""){
      wx.navigateTo({
        url: "/pages/component/login/login"
      });
    }else{
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../sharespace/sharespace?id=' + id,
      })
    }
  },

  toHomepage: function (e) {
    if (this.data.openid == null || this.data.openid == "") {
      wx.navigateTo({
        url: "/pages/component/login/login"
      });
    } else {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../homepage/homepage?id=' + id,
      })
    }
  },




})